function read()
{
	var xmlhttp;
	var response;
	var ledState;
	if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}
	else
		{// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			//returned response converted to int
			response = parseInt(xmlhttp.responseText,10);
			//corresponding int to ON or OFF string
			ledState=(response==true)?"ON":"OFF";
			//update text with id led_state
			document.getElementById("led_state").innerHTML=ledState;
		}
	}
	xmlhttp.open("GET","php/read.php",true);
	xmlhttp.send(null);
}
	
function write(str)
{
	var xmlhttp;
	var response;
	if (window.XMLHttpRequest)
		{// code for IE7+, Firefox, Chrome, Opera, Safari
			xmlhttp=new XMLHttpRequest();
		}
	else
		{// code for IE6, IE5
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4 && xmlhttp.status==200)
		{
			//corresponding int to ON or OFF string
			ledState=(parseInt(str,10)==true)?"ON":"OFF";
			//update text with id led_state
			document.getElementById("led_state").innerHTML=ledState;
		}
	}
	xmlhttp.open("GET","php/write.php?q="+str,true);
	xmlhttp.send();
}
	
function load()
{
	read();
}

function toggleled()
{
	var ledStatetxt;
	var ledState;
	//Read the current state to make sure most up-to-date one is in textfield
	read();
	//Get the current state from textfield
	ledStatetxt = document.getElementById("led_state").innerHTML;
	//Toggle the state from on to off and vice-versa
	ledState=(ledStatetxt==="ON")?"0":"1";
	//Write the new state which also updates textfield
	write(ledState);
}
	